package com.yidu.ruifeng_logistics.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidu.ruifeng_logistics.entity.LogisticsCourier;
import com.yidu.ruifeng_logistics.entity.LogisticsDriver;
import com.yidu.ruifeng_logistics.entity.LogisticsStaff;
import com.yidu.ruifeng_logistics.service.LogisticsCourierService;
import com.yidu.ruifeng_logistics.service.LogisticsDriverService;
import com.yidu.ruifeng_logistics.service.LogisticsStaffService;
import com.yidu.ruifeng_logistics.utils.BaseMessage;
import com.yidu.ruifeng_logistics.vo.CourierVo;
import com.yidu.ruifeng_logistics.vo.DriverVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 快递员表(LogisticsCourier)表控制层
 *
 * @author 肖楚科
 * @since 2021-03-18 15:43:29
 */
@RestController
@RequestMapping("logisticsCourier")
public class LogisticsCourierController {
    /**
     * 服务对象
     */
    @Resource
    private LogisticsCourierService logisticsCourierService;

    @Resource
    private LogisticsStaffService logisticsStaffService;

    @Resource
    private LogisticsDriverService logisticsDriverService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据id查询司机", notes = "返回仓库对象")
    @PostMapping("/showUpdate")
    public Map<String, Object> selectOne(Integer id) {
        Map<String, Object> map = new HashMap<>();
        LogisticsCourier courier = logisticsCourierService.queryById(id);
        LogisticsStaff staff = logisticsStaffService.queryById(courier.getStaffId());
        //code为后台判断用
        map.put("code", 0);
        //data为根据id查询的快递员
        map.put("data", courier);
        //staffName为根据快递员外键的员工id查询的与员工对象的员工名字
        map.put("staffName", staff.getStaffName());
        return map;
    }

    /**
     * 根据条件查询司机
     *
     * @param courierVo 司机对象
     * @return 信息对象
     */
    @ApiOperation(value = "根据条件查询全部司机", notes = "返回状态信息")
    @GetMapping("/queryAll")
    public Map<String, Object> queryAll(CourierVo courierVo, int page, int limit, String keyWord) {
        //将参数传入到分页助手
        PageHelper.startPage(page, limit);
        //将关键字参数填入快递员vo对象中，用于查询
        courierVo.setKeyWord(keyWord);
        //全查出快递员集合
        List<CourierVo> list = logisticsCourierService.queryAll(courierVo);
        //new一个map用于返回状态信息
        Map<String, Object> map = new HashMap<>();
        //将全查得到的CourierVo集合传入PageInfo中用于分页
        PageInfo<CourierVo> pageInfo = new PageInfo<>(list);
        //传递code和msg信息
        map.put("code", 0);
        map.put("msg", "查询成功");
        //data为使用pageinfo对象得到的所需集合
        map.put("data", pageInfo.getList());//可以直接写list
        //count使用pageinfo对象得到的所需行数
        map.put("count", pageInfo.getTotal()); //总行数
        return map;
    }

    /**
     * 批量删除方法
     *
     * @param ids id拼接字符串
     * @return 状态信息
     */
    @ApiOperation(value = "批量删除快递员", notes = "返回状态信息")
    @PostMapping("/deleteAll")
    public BaseMessage deleteAll(String ids) {
        return logisticsCourierService.deleteAll(ids) > 0 ? BaseMessage.success() : BaseMessage.fail();
    }

    /**
     * 根据id新增或者修改快递员信息
     *
     * @param logisticsCourier 快递员对象
     * @return 状态信息
     */
    @ApiOperation(value = "增加或修改快递员", notes = "返回状态信息")
    @PostMapping("/addOrUpdate")
    public BaseMessage addOrUpdate(LogisticsCourier logisticsCourier) {
        //判断id是否为空，来进行增加或修改
        if (StringUtils.isEmpty(logisticsCourier.getCourierId())) {
            logisticsCourierService.insert(logisticsCourier);
        } else {
            logisticsCourierService.update(logisticsCourier);
        }
        return BaseMessage.success();
    }

    /**
     * 查出未关联的员工
     *
     * @return 返回状态信息
     */
    @ApiOperation(value = "修改时查出未关联的员工", notes = "返回状态信息")
    @GetMapping("/queryStaff")
    public BaseMessage queryStaff(Integer id) {
        //将员工，司机，快递员全部查出
        List<LogisticsStaff> list = logisticsStaffService.queryAll(new LogisticsStaff());
        List<DriverVo> driverList = logisticsDriverService.queryAll(new DriverVo());
        List<CourierVo> logisticsCouriers = logisticsCourierService.queryAll(new CourierVo());
        //id长字符串
        String ids = "";
        //计数器
        int i = 0;
        //遍历司机，将id累加
        for (DriverVo driverVo : driverList) {
            //i为0时,不加逗号分隔
            if (i == 0) {
                ids += driverVo.getStaffId();
            } else {
                ids += "," + driverVo.getStaffId();
            }
            i++;
        }
        //遍历快递员，将id累加
        for (CourierVo courier : logisticsCouriers) {
            //修改时回显选中的员工忽略
            if (!courier.getStaffId().equals(logisticsCourierService.queryById(id).getStaffId())) {
                if (StringUtils.hasText(ids)) {
                    ids += "," + courier.getStaffId();
                } else {
                    ids += courier.getStaffId();
                }
            }
        }
        //员工集合的迭代器
        Iterator<LogisticsStaff> iterator = list.iterator();
        //遍历迭代器
        while (iterator.hasNext()) {
            //如果id长字符串中包含这次员工id，就移除，剩下的都为未关联的员工
            if (ids.contains(String.valueOf(iterator.next().getStaffId()))) {
                iterator.remove();
            }
        }

        return BaseMessage.success(list);
    }

    @ApiOperation(value = "增加时查出未关联的员工", notes = "返回状态信息")
    @GetMapping("/queryStaff2")
    public BaseMessage queryStaff() {
        //将员工，司机，快递员全部查出
        List<LogisticsStaff> list = logisticsStaffService.queryAll(new LogisticsStaff());
        List<DriverVo> driverList = logisticsDriverService.queryAll(new DriverVo());
        List<CourierVo> logisticsCouriers = logisticsCourierService.queryAll(new CourierVo());
        //id长字符串
        String ids = "";
        //计数器
        int i = 0;
        //遍历司机，将id累加
        for (DriverVo driverVo : driverList) {
            //i为0时,不加逗号分隔
            if (i == 0) {
                ids += driverVo.getStaffId();
            } else {
                ids += "," + driverVo.getStaffId();
            }
            i++;
        }
        //遍历快递员，将id累加
        for (CourierVo courier : logisticsCouriers) {
            if (StringUtils.hasText(ids)) {
                ids += "," + courier.getStaffId();
            } else {
                ids += courier.getStaffId();
            }
        }
        //员工集合的迭代器
        Iterator<LogisticsStaff> iterator = list.iterator();
        while (iterator.hasNext()) {
            //如果id长字符串中包含这次员工id，就移除，剩下的都为未关联的员工
            if (ids.contains(String.valueOf(iterator.next().getStaffId()))) {
                iterator.remove();
            }
        }
        return BaseMessage.success(list);
    }

}
